import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { HttpService } from '../../../service/httpService';

@Component({
  selector: 'page-games',
  templateUrl: 'games.html',
})
export class GamesPage {
  private games:any;
  private showLoading:boolean = true;
  private size:number = 28;
  private page:number = 0;
  private platform:string = '';
  constructor(public navCtrl: NavController,
              public http:HttpService,
              public navParams: NavParams) {

    this.platform = navParams.get("platform");
    this.getGames();

  }

  getGames(){
    let map = {
      isMobile:true,
      isHot:true,
      platform:this.platform ? this.platform : '',
      page:this.page,
      pageSize:this.size
    };

    var  url = "/api/v2/Game/GetHotGame";
    let self = this;
    //获取热门游戏
    this.http.get(url,map,function (res,err) {
      self.showLoading = false;
      if(!err){
        let obj = res.list;
        for(var i=0;i<obj.length;i++){
          obj[i].ImageUrl = self.http.imageUrl+obj[i].ImageUrl;
        }
        self.games = obj;
      }
    });
  }

  doInfinite(e){
    this.page ++;
    let map = {
      isMobile:true,
      isHot:true,
      platform:this.platform ? this.platform : '',
      pageIndex:this.page,
      pageSize:this.size
    };
    var  url = "/api/v2/Game/GetHotGame";
    let self = this;
    //获取热门游戏
    this.http.get(url,map,function (res,err) {
      self.showLoading = false;
      if(!err){
        let obj = res.list;
        for(var i=0;i<obj.length;i++){
          obj[i].ImageUrl = self.http.imageUrl+obj[i].ImageUrl;
          self.games.push(obj[i]);
        }
        e.complete();
        if(obj.length<28){
          e.enable(false);
        }
      }
    });
  }
}
